package com.protocols.sctp.m3ua.sccp.tcap.camel;

import java.io.Serializable;

import com.protocols.BCDNumber;
import com.protocols.OctaveInteger;
import com.protocols.ProtoOperations;
import com.protocols.ProtocolStackException;
import com.protocols.sctp.m3ua.sccp.tcap.TcapBegin;
import com.protocols.sctp.m3ua.sccp.tcap.TcapConstructor;
import com.protocols.sctp.m3ua.sccp.tcap.TcapPrimitiveUnitByteArray;
import com.protocols.sctp.m3ua.sccp.tcap.TcapPrimitiveUnitNumber;

public class CamelInitialDP extends CamelInvoke implements Serializable{
	
	private TcapConstructor Parameters = new TcapConstructor(0x30);
	private TcapPrimitiveUnitNumber ServiceKey = new TcapPrimitiveUnitNumber(0x80);
	private TcapPrimitiveUnitByteArray CallingPartyNumber = new TcapPrimitiveUnitByteArray(0x83);
	private TcapPrimitiveUnitNumber CallingPartyCategory = new TcapPrimitiveUnitNumber(0x85);
	private TcapPrimitiveUnitNumber LocationNumber = new TcapPrimitiveUnitNumber(0x8a);
	private TcapConstructor BearerCapability = new TcapConstructor(0xbb);
	private TcapPrimitiveUnitNumber BearerCap= new TcapPrimitiveUnitNumber(0x80);
	private TcapPrimitiveUnitNumber EvetTypeBCSM= new TcapPrimitiveUnitNumber(0x9c);
	private TcapPrimitiveUnitByteArray IMSI= new TcapPrimitiveUnitByteArray(0x9f);
	private TcapConstructor LocationInfo = new TcapConstructor(0xbf);
	private TcapPrimitiveUnitNumber AgeofLocationInfo= new TcapPrimitiveUnitNumber(0x02);	
	private TcapPrimitiveUnitByteArray VLRNumber = new TcapPrimitiveUnitByteArray(0x81);	
	private TcapConstructor CellIDorLAIContainer= new TcapConstructor(0xa3);
	private TcapPrimitiveUnitNumber CellID= new TcapPrimitiveUnitNumber(0x80);
	private TcapPrimitiveUnitNumber LAI= new TcapPrimitiveUnitNumber(0x81);
	private TcapConstructor ExtBasicServiceCodeContainer= new TcapConstructor(0xbf);
	private TcapPrimitiveUnitNumber ExtBasicServiceCode= new TcapPrimitiveUnitNumber(0x83);	
	private TcapPrimitiveUnitNumber CallRefNumber= new TcapPrimitiveUnitNumber(0x9f);	
	private TcapPrimitiveUnitByteArray MSCNumber = new TcapPrimitiveUnitByteArray(0x9f);	
	private TcapPrimitiveUnitByteArray CalledPartyNumber = new TcapPrimitiveUnitByteArray(0x81);	
	private TcapPrimitiveUnitByteArray TimeAndTimeZone = new TcapPrimitiveUnitByteArray(0x9f);
	
	public CamelInitialDP() {
		super();		
		super.Invoke.addElement(Parameters);
		Parameters.addElement(ServiceKey);
		Parameters.addElement(CallingPartyNumber);
		Parameters.addElement(CallingPartyCategory);
		Parameters.addElement(LocationNumber);
		Parameters.addElement(BearerCapability);
		BearerCapability.addElement(BearerCap);
		Parameters.addElement(EvetTypeBCSM);
		Parameters.addElement(IMSI);
		Parameters.addElement(LocationInfo);
		LocationInfo.addElement(AgeofLocationInfo);
		LocationInfo.addElement(VLRNumber);
		LocationInfo.addElement(CellIDorLAIContainer);
		CellIDorLAIContainer.addElement(CellID);
		CellIDorLAIContainer.addElement(LAI);
		Parameters.addElement(ExtBasicServiceCodeContainer);
		ExtBasicServiceCodeContainer.addElement(ExtBasicServiceCode);
		Parameters.addElement(CallRefNumber);
		Parameters.addElement(MSCNumber);
		Parameters.addElement(CalledPartyNumber);
		Parameters.addElement(TimeAndTimeZone);
		
		setOpCode(0,true);	// initial DP
		ServiceKey.setTagValue(20);		
		IMSI.setTagCode(0x32);
		LocationInfo.setTagCode(0x34);
		ExtBasicServiceCodeContainer.setTagCode(0x35);
		CallRefNumber.setTagCode(0x36);			
		MSCNumber.setTagCode(0x37);
		CalledPartyNumber.setTagCode(0x38);
		TimeAndTimeZone.setTagCode(0x39);
	}
		
	public long getServiceKey() {
		return ServiceKey.getTagValue();
	}

	public void setServiceKey(long serviceKey) {
		ServiceKey.setTagValue(serviceKey);
	}

	public int getCallingPartyNumberInfo() {
		return ProtoOperations.intFromByteArray(CallingPartyNumber.getTagValue(), 0, 2);
	}


	public String getCallingPartyNumber() {
		
		byte [] val = CallingPartyNumber.getTagValue();
		
		if (val.length>2){
			Boolean odd=((val[0] & 128)==128);
			int i=2;
			String value="";
			while (i<val.length){
				value+=(val[i] & 0b00001111);
				value+=(val[i] & 0b11110000)>>4;
				i++;			
			}			
			return ( odd ? value.substring(0, value.length()-1):value);
		}
			
		
		return "";
	}

	public void setCallingPartyNumber(int callingPartyNumberInfo,String callingPartyNumber) {
		callingPartyNumberInfo= ( (callingPartyNumber.length()%2<<15)|callingPartyNumberInfo);
		OctaveInteger callingpartinfo = new OctaveInteger(callingPartyNumberInfo, 2);
		BCDNumber callingnumber = new BCDNumber(callingPartyNumber, "f");		
		
		CallingPartyNumber.setTagValue(ProtoOperations.concatArrays(callingpartinfo.getByteArray(), callingnumber.getByteArray()));
	}



	public long getCallingPartyCategory() {
		return CallingPartyCategory.getTagValue();
	}

	public void setCallingPartyCategory(long callingPartyCategory) {
		CallingPartyCategory.setTagValue( callingPartyCategory);
	}

	public long getLocationNumber() {
		return LocationNumber.getTagValue();
	}

	public void setLocationNumber(long locationNumber) {
		LocationNumber.setTagValue(locationNumber);
	}

	public long getBearerCap() {
		return BearerCap.getTagValue();
	}

	public void setBearerCap(long bearerCap) {
		BearerCap.setTagValue( bearerCap);
	}

	public long getEvetTypeBCSM() {
		return EvetTypeBCSM.getTagValue();
	}

	public void setEvetTypeBCSM(long evetTypeBCSM) {
		EvetTypeBCSM.setTagValue( evetTypeBCSM);
	}

	public String getIMSI() {
		
		byte [] val = IMSI.getTagValue();
		
		int i=0;
		String value="";
		while (i<val.length){
			value+=(val[i] & 0b00001111);
			value+=(val[i] & 0b11110000)>>4;
			i++;			
		}			
		return value;

	}

	public void setIMSI(String iMSI) {
		BCDNumber imsi = new BCDNumber(iMSI, "f");		
		
		IMSI.setTagValue(imsi.getByteArray());
	}

	public long getAgeofLocationInfo() {
		return AgeofLocationInfo.getTagValue();
	}

	public void setAgeofLocationInfo(long ageofLocationInfo) {
		AgeofLocationInfo.setTagValue( ageofLocationInfo);
	}



	public int getVLRNumberInfo() {
		return ProtoOperations.intFromByteArray(VLRNumber.getTagValue(), 0, 1);
	}


	public String getVLRNumber() {
		
		byte [] val = VLRNumber.getTagValue();
		
		if (val.length>1){
			int i=1;
			String value="";
			while (i<val.length){
				value+=(val[i] & 0b00001111);
				value+=(val[i] & 0b11110000)>>4;
				i++;			
			}			
			return value;
		}
			
		
		return "";
	}

	public void setVLRNumber(int vLRNumberInfo,String vLRNum) {
		BCDNumber vlrno = new BCDNumber(vLRNum, "f");
		
		VLRNumber.setTagValue(ProtoOperations.concatArrays(new byte []{(byte)vLRNumberInfo}, vlrno.getByteArray()));
	}
	
	
	public long getExtBasicServiceCode() {
		return ExtBasicServiceCode.getTagValue();
	}

	public void setExtBasicServiceCode(long extBasicServiceCode) {
		ExtBasicServiceCode.setTagValue( extBasicServiceCode);
	}

	public long getCallRefNumber() {
		return CallRefNumber.getTagValue();
	}

	public void setCallRefNumber(long callRefNumber) {
		CallRefNumber.setTagValue(callRefNumber);
	}

	public int getMSCNumberInfo() {
		return ProtoOperations.intFromByteArray(MSCNumber.getTagValue(), 0, 1);
	}


	public String getMSCNumber() {
		
		byte [] val = MSCNumber.getTagValue();
		
		if (val.length>1){
			int i=1;
			String value="";
			while (i<val.length){
				value+=(val[i] & 0b00001111);
				value+=(val[i] & 0b11110000)>>4;
				i++;			
			}			
			return value;
		}
			
		
		return "";
	}

	public void setMSCNumber(int mscNumberInfo,String mscNumber) {
		BCDNumber mscno = new BCDNumber(mscNumber, "f");		
		MSCNumber.setTagValue(ProtoOperations.concatArrays(new byte []{(byte)mscNumberInfo}, mscno.getByteArray()));
	}
	


	public int getCalledPartyInfo() {
		return ProtoOperations.intFromByteArray(CalledPartyNumber.getTagValue(), 0, 1);
	}

	public String getCalledNumber() {
		
		byte [] val = CalledPartyNumber.getTagValue();
		
		if (val.length>1){
			int i=1;
			String value="";
			while (i<val.length){
				value+=(val[i] & 0b00001111);
				value+=(val[i] & 0b11110000)>>4;
				i++;			
			}			
			return value;
		}
			
		
		return "";
	}

	public void setCalledNumber(int calledPartyNumberInfo,String calledPartyNumber) {
		BCDNumber calledparty = new BCDNumber(calledPartyNumber, "f");
		
		CalledPartyNumber.setTagValue(ProtoOperations.concatArrays(new byte []{(byte)calledPartyNumberInfo}, calledparty.getByteArray()));
	}	

	public String getTimeInfo() {
		
		byte [] val = TimeAndTimeZone.getTagValue();
		
		if (val.length>1){
			int i=0;
			String value="";
			while (i<val.length){
				value+=(val[i] & 0b00001111);
				value+=(val[i] & 0b11110000)>>4;
				i++;			
			}			
			return value;
		}
			
		
		return "";
	}

	public void setTimeInfo(String timeinfo){
		BCDNumber timestamp = new BCDNumber(timeinfo, "f");
		
		TimeAndTimeZone.setTagValue(timestamp.getByteArray());
	}	
	
	public long getCellID() {		
		return CellID.getTagValue();		

	}

	public void setCellID(long cellid){
		CellID.setTagValue(cellid);
		LAI.setVisible(false);
	}	
	
	public long getLAI() {		
		return LAI.getTagValue();		

	}

	public void setLAI(long lai){
		LAI.setTagValue(lai);
		CellID.setVisible(false);
	}	
	
	public TcapConstructor getRootComponent(){
		return Invoke;
	}
	
	
	
}
